# -*- encoding=utf8 -*-
"""
RedFlag-TC0000114  特效添加用例
①进入特效面板
②下载特效
③添加特效
④在特效面板中切换特效场景
⑤点击无场景
⑦点击X场景
⑧在特效面板页播放/暂停特效场景
"""
__author__ = "ldm"

import sys
import os

# 获取当前脚本所在目录.py
current_dir = os.path.dirname(os.path.abspath(__file__))
# 获取上级目录.air
parent_dir = os.path.dirname(current_dir)
# 获取上级目录air
grandparent_dir = os.path.dirname(parent_dir)
# 获取和air目录平级的conf、testcase目录路径
conf_dir = os.path.join(grandparent_dir, 'conf')
testcase_dir = os.path.join(grandparent_dir, 'testcase')
# 将conf、testcase目录添加到sys.path
sys.path.append(conf_dir)
sys.path.append(testcase_dir)

from PageFunction import CameraVideoShooter
from elements import *

auto_setup(__file__)
shooter = CameraVideoShooter()

log("--------------------------------进入特效面板--------------------------------------")
"""
拍摄进入装饰页
点击特效
点击添加
断言
"""

shooter.enter_photography_decoration_page()
shooter.click_element(decoration_page_special_effects)
shooter.click_element(decoration_page_special_effects_add)
shooter.assert_element_exists(decoration_page_special_effects_panel_title, "断言失败，特效tetle不存在没有进入特效面板")


log("--------------------------------下载特效场景--------------------------------------")
"""
通过循环依次点击特效
"""

for loop in range(3):
    print(f"开始第 {loop + 1} 次循环")

    for i in range(12):
        element = decoration_page_special_effects_panel_choice[i]
        if not element.exists():
            print(f"未找到索引为 {i} 的特效元素，退出循环")
            break

        # 获取子元素
        download_item = element.child("cn.migu.video.clip.walle:id/clip_tv_item")
        download_img = element.offspring("cn.migu.video.clip.walle:id/clip_iv_item_img_download")

        # 首先检查 download_item 是否存在
        if not download_item.exists():
            print(f"索引 {i} 的 download_item 不存在，跳过该节点")
            continue  # 跳过当前特效，继续下一个

        # 如果 download_item 存在，点击它
        shooter.click_element(download_item)
        sleep(1)  # 等待一下，让界面有反应

        # 检查 download_img 是否存在
        if download_img.exists():
            print(f"索引 {i} 开始下载，等待完成")
            # 等待下载完成（download_img消失）
            result = shooter.wait_for_element_disappearance(download_img, timeout=30)
            if not result:
                print(f"索引 {i} 下载超时，跳过该特效")
                continue
            else:
                print(f"索引 {i} 下载完成")
        else:
            print(f"索引 {i} 已经下载完成，无需再次下载")

        # 验证下载确实完成
        shooter.assert_element_not_exists(
            download_img,
            f"断言失败，索引 {i} 的下载按钮仍存在"
        )
        print(f"索引 {i} 的特效下载成功")

    if loop < 2:
        print("滑动屏幕")
        shooter.swipe_screen((0.45, 0.9), (0.45, 0.68))
        sleep(2)


log("--------------------------------添加特效场景--------------------------------------")
"""
点击✓
断言
"""

shooter.click_element(decoration_page_special_effects_panel_confirm)
shooter.assert_image_exists(r"tpl1757924081395.png", "断言失败，预期图片并未出现，添加特效失败", record_pos=(-0.176, 0.752), resolution=(1224, 2700), threshold=0.95)
shooter.click_element(decoration_page_special_effects_add)


log("--------------------------------在特效面板中切换特效场景--------------------------------------")
"""
点击添加
选择一个特效
获取特效时间
断言
切换另一个特效
获取特效时间
断言
"""

shooter.click_element(decoration_page_special_effects_add)
shooter.click_element(decoration_page_special_effects_panel_choice[5])
sleep(3.0)
time1 = shooter.get_element_text(decoration_page_special_effects_playback_time)
shooter.assert_texts_equal(time1, "00:05", "断言失败，两个值不一致")
shooter.click_element(decoration_page_special_effects_panel_choice[6])
sleep(3.0)
time2 = shooter.get_element_text(decoration_page_special_effects_playback_time)
shooter.assert_texts_equal(time2, "00:05", "断言失败，两个值不一致")


log("--------------------------------点击无场景--------------------------------------")
"""
执行滑动操作，返回页面最上方
点击第十一个特效
截图
点击无
截图
断言
"""

shooter.swipe_screen((0.45,0.76),(0.45,0.90),1,3)
shooter.click_element(decoration_page_special_effects_panel_choice[0])
shooter.assert_image_exists(r"tpl1758007734211.png", "断言失败，预期图片并未出现", record_pos=(-0.394, 0.572), resolution=(1224, 2700),threshold=0.95)


log("--------------------------------点击X场景--------------------------------------")
"""
点击第一个特效
点击X
断言
"""

shooter.click_element(decoration_page_special_effects_panel_choice[1])
shooter.click_element(decoration_page_special_effects_panel_cancel)
shooter.assert_element_not_exists(decoration_page_special_effects_panel_confirm, "断言失败，预期元素出现")


log("--------------------------------在特效面板页播放/暂停特效场景--------------------------------------")
"""
点击添加
点击播放
断言
点击暂停
断言
"""

shooter.click_element(decoration_page_special_effects_add)
shooter.click_element(decoration_page_special_effects_play)
shooter.assert_image_exists(r"tpl1758007987539.png", "断言失败，预期图片并未出现", record_pos=(0.0, 0.3), resolution=(1224, 2700))
shooter.click_element(decoration_page_special_effects_playback_time)
shooter.assert_image_exists(r"tpl1758007999455.png", "断言失败，预期图片并未出现", record_pos=(0.0, 0.3), resolution=(1224, 2700))


# 关闭应用
stop_app(UGC_package_name)